package model;

import java.sql.*;
import java.util.ArrayList;

import myutil.Connect;

import vo.LoginInfoVo;

/**
 * @author Sony
 * 
 */

public class LoginMo {
	ArrayList<LoginInfoVo> loginInfo = null;

	public ArrayList<LoginInfoVo> authenticate(String username,
			String password, String site) throws Exception {
		// String site = null;
		Connect c = new Connect();
		c.site = site;
		String user_type = "";
		String searchLoginDetailsQuery;
		searchLoginDetailsQuery = "select * from login_info where login_id='"
				+ username + "' and password='" + password + "'";
//System.out.println(searchLoginDetailsQuery);
		ResultSet rs = c.getResult(searchLoginDetailsQuery);
		if (rs.next()) {
			loginInfo = new ArrayList<LoginInfoVo>();
			rs.previous();
		}

		while (rs.next()) {
			LoginInfoVo logininfo = new LoginInfoVo();
			logininfo.setLogin_id(rs.getString("login_id"));
			logininfo.setUser_type(rs.getString("user_type"));
			user_type=rs.getString("user_type");
			
			String query;
			if (user_type.equals("student")) {
				query="select student_id from student_details where email_id='"+username+"'";
				//System.out.println(query);
				ResultSet rs2 = c.getResult(query);
				while(rs2.next()){
				logininfo.setUser_id(rs2.getInt("student_id"));}
			}
			else if(user_type.equals("staff"))
			{
				query="select staff_id from college_staff where email_id='"+username+"'";
				ResultSet rs2 = c.getResult(query);
				while(rs2.next()){
				logininfo.setUser_id(rs2.getInt("staff_id"));}
			}
			else if(user_type.equals("admin"))
			{
				logininfo.setUser_id(0);
			}
			loginInfo.add(logininfo);
			System.out.println("added the login info");
		
		}

		

		return loginInfo;

	}
}
